import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../page/Login.vue"
import Layout from '../page/Layout/Index.vue'
import ActiveCode from '../page/ActiveCode.vue'
import Users from '../page/Users.vue'
import Addactivecode from '../page/Addactivecode.vue'
import Childrencode from '../page/Childrencode.vue'
import Domain from '../page/Domain.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: "/layout"
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/layout',
      name: 'layout',
      component: Layout,
      children: [
        {
          path: '/',
          redirect: "/activecode"
        },
        {
          path: '/activecode',
          name: 'activecode',
          component: ActiveCode
        },

        {
          path: '/addactivecode',
          name: 'addactivecode',
          component: Addactivecode
        },
        {
          path: '/childrencode',
          name: 'childrencode',
          component: Childrencode
        },


      ]

    },

  ]
})


let userrouter = {
  path: '/user',
  name: 'user',
  component: Users
}
let domainrouter = {
  path: '/domain',
  name: 'domain',
  component: Domain
}


export function currentRoutes() {

  const currentRoutes = router.options.routes
  if (sessionStorage.getItem('username') == 'admin') {
    currentRoutes[2].children.push(userrouter)
    currentRoutes[2].children.push(domainrouter)
    router.addRoutes(currentRoutes)
  }

}




router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    return next()
  } else {
    const tokenStr = sessionStorage.getItem("token")
    if (tokenStr !== null) {
      next()
    } else {
      next('/login')
    }
  }

})



export default router

